unit glFileTxt;

interface

uses Textures, SysUtils, Windows, Forms, dfHGL, dfMath;

type
  TFace = Record
    V1, V2, V3, V4: Integer;
    U, V: Single;
    TextureIndex: Integer;
  end;

  TFileTxt = class(TObject)
    Texture: Array of integer;
    TextureCount: Integer;
    VertexCount: Integer;
    FaceCount: Integer;
    Vertex: Array of TdfVec3f;
    Face: Array of TFace;
  private

  public
    constructor Create;
    procedure LoadMap(Filenam: TFileName);
    procedure glDraw();

  end;

var

  rquad, rtri, rpol: single;
  FileTxt: TFileTxt;

Function DrawGLScene(): Boolean;
procedure testglcube;

//procedure glBindTexture(target: GLenum; Texture: glUint); stdcall;external Opengl32;

implementation

constructor TFileTxt.Create;
begin

end;

procedure testglcube;
begin
  gl.Beginp(GL_TRIANGLES);
  gl.vertex3f(1, 0, 0);
  gl.vertex3f(0, 1, 0);
  gl.Endp;
end;

Function DrawGLScene(): Boolean;
begin
  gl.Clear(GL_COLOR_BUFFER_BIT);// or GL_DEPTH_BUFFER_BIT);
  gl.LoadIdentity();
  gl.Translatef(-0.5, 0.0, -15.0);
  gl.Rotatef(rquad, 0.0, 1.0, 1.0);
  gl.Beginp(GL_QUADS);
  gl.Color3f(0.0, 1.0, 0.0);
  gl.vertex3f(0.5, 0.5, -0.5); // top left
  gl.vertex3f(-0.5, 0.5, -0.5); // top right
  gl.vertex3f(-0.5, 0.5, 0.5); // bottom right
  gl.vertex3f(0.5, -0.5, 0.5); // bottom left

  gl.Color3f(1.0, 0.0, 0.0);
  gl.vertex3f(0.5, 0.5, 0.5); // top left
  gl.vertex3f(-0.5, 0.5, 0.5); // top right
  gl.vertex3f(-0.5, -0.5, 0.5); // bottom right
  gl.vertex3f(0.5, -0.5, 0.5); // bottom left

  gl.Color3f(1.0, 1.0, 0.0);
  gl.vertex3f(0.5, -0.5, -0.5); // top left
  gl.vertex3f(-0.5, -0.5, -0.5); // top right
  gl.vertex3f(-0.5, 0.5, -0.5); // bottom right
  gl.vertex3f(0.5, 0.5, -0.5); // bottom left

  gl.Color3f(0.0, 0.0, 1.0);
  gl.vertex3f(-0.5, 0.5, 0.5); // top left
  gl.vertex3f(-0.5, 0.5, -0.5); // top right
  gl.vertex3f(-0.5, -0.5, -0.5); // bottom right
  gl.vertex3f(-0.5, -0.5, 0.5); // bottom left

  gl.Color3f(1.0, 0.0, 1.0);
  gl.vertex3f(0.5, 0.5, -0.5); // top left
  gl.vertex3f(0.5, 0.5, 0.5); // top right
  gl.vertex3f(0.5, -0.5, 0.5); // bottom right
  gl.vertex3f(0.5, -0.5, -0.5); // bottom left
  gl.Endp();

  gl.Rotatef(-rquad, 0.0, 1.0, 1.0);
  gl.Rotatef(rtri, 0.0, 1.0, 0.0);
  gl.Translatef(0.0, 0.0, 3.0);
  gl.Rotatef(rtri, 0.0, 0.0, 1.0);

  gl.Beginp(GL_TRIANGLES);
  gl.Color3f(1.0, 0.0, 0.0); // red
  gl.vertex3f(0.0, 0.5, 0.0); // top
  gl.Color3f(0.0, 1.0, 0.0); // green
  gl.vertex3f(-0.5, -0.5, 0.5); // left
  gl.Color3f(0.0, 0.0, 1.0); // blue
  gl.vertex3f(0.5, -0.5, 0.5); // right
  gl.Color3f(1.0, 0.0, 0.0); // red
  gl.vertex3f(0.0, 0.5, 0.0); // top of
  gl.Color3f(0.0, 0.0, 1.0); // blue
  gl.vertex3f(0.5, -0.5, 0.5); // left of
  gl.Color3f(0.0, 1.0, 0.0); // red
  gl.vertex3f(0.5, -0.5, -0.5); // right of
  gl.Color3f(1.0, 0.0, 0.0); // red
  gl.vertex3f(0.0, 0.5, 0.0); // top of
  gl.Color3f(0.0, 1.0, 0.0); // green
  gl.vertex3f(0.5, -0.5, -0.5); // left of
  gl.Color3f(0.0, 0.0, 1.0); // blue
  gl.vertex3f(-0.5, -0.5, -0.5); // right of
  gl.Color3f(1.0, 0.0, 0.0); // red
  gl.vertex3f(0.0, 0.5, 0.0); // top of
  gl.Color3f(0.0, 0.0, 1.0); // blue
  gl.vertex3f(-0.5, -0.5, -0.5); // left of
  gl.Endp();

  gl.Loadidentity(); // moveto 0,0,0
  gl.Translatef(15.0, 0.0, -6.0);
  gl.Rotatef(rpol, 0.0, 0.0, 1.0);
  gl.Color3f(0.0, 0.0, 1.0); // blue

  gl.Beginp(GL_POLYGON);
  gl.vertex3f(-0.5, 0.5, 0.0); // top left
  gl.vertex3f(0.0, 0.75, 0.0); // top right
  gl.vertex3f(0.5, 0.5, 0.0); // bottom right
  gl.vertex3f(0.5, -0.5, 0.0); // bottom left

  gl.vertex3f(0.0, -0.75, 0.0); // top left
  gl.vertex3f(-0.5, -0.5, 0.0); // top right
  gl.Endp();
  rtri := rtri + 0.02;
  rquad := rquad + 0.02;
  rpol := rpol + 0.02;
  result := true;
end;

{ ------------------------------------------------------------------ }
{ Load the map info from the map.txt files }
{ ------------------------------------------------------------------ }
Function GetPathNameFile(Filenam: TFileName):String;
Begin
  while (Filenam[length(Filenam)]<>'/') do
    delete(Filenam,length(Filenam),1);
  result:=Filenam;
End;

procedure TFileTxt.LoadMap(Filenam: TFileName);
var
  F: Textfile;
  Tex: Array of String;
  S: String;
  I, J: Integer;
begin
  if not FileExists(Filenam) then
    exit;
  AssignFile(F, Filenam);
  Reset(F);

  // Load the textures
  Readln(F, TextureCount);
  SetLength(Tex, TextureCount);
  SetLength(Texture, TextureCount);
  for I := 0 to TextureCount - 1 do
  begin
    Readln(F, S);
    Tex[I] := trim(Copy(S, 1, Pos(' ', S) - 1));
    S := trim(Copy(S, Pos(' ', S) + 1, length(S)));
    //S := GetPathNameFile(Filenam) + trim(Copy(S, Pos(' ', S) + 1, length(S)));
    Texture[I]:=renderTexLoad(s);
    //LoadTexture(S, Texture[I], FALSE);
  end;

  // Load the vertices
  Readln(F, VertexCount);
  SetLength(Vertex, VertexCount);
  for I := 0 to VertexCount - 1 do
    Readln(F, Vertex[I].X, Vertex[I].Y, Vertex[I].Z);

  // Load the faces
  Readln(F, FaceCount);
  SetLength(Face, FaceCount);
  for I := 0 to FaceCount - 1 do
  begin
    Readln(F, Face[I].V1, Face[I].V2, Face[I].V3, Face[I].V4, Face[I].U,
      Face[I].V, S);
    S := trim(Copy(S, 1, 12));

    for J := 0 to TextureCount - 1 do
      if Tex[J] = S then
        Face[I].TextureIndex := J;
  end;
  CloseFile(F);
end;

procedure TFileTxt.glDraw();
var
  I: Integer;
begin
  For I := 0 to FaceCount - 1 do
    with Face[I] do
    begin
      gl.BindTexture(GL_TEXTURE_2D, Texture[TextureIndex]);
      gl.Beginp(GL_QUADS);
      gl.TexCoord2f(0, 0);
      gl.Vertex3fv(Vertex[V1 - 1]);
      gl.TexCoord2f(U, 0);
      gl.Vertex3fv(Vertex[V2 - 1]);
      gl.TexCoord2f(U, V);
      gl.Vertex3fv(Vertex[V3 - 1]);
      gl.TexCoord2f(0, V);
      gl.Vertex3fv(Vertex[V4 - 1]);
      gl.Endp();
      // application.ProcessMessages;
    end;
end;

end.
